package borge.web.teamspirit.client.data;

import java.util.Date;

import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import borge.web.teamspirit.client.TeamSpirit;
import borge.web.teamspirit.client.interfaces.PersistentData;
import borge.web.teamspirit.client.interfaces.UserContentProvider;

/**
 * Represents a text comment stamped with poster and date.
 * Can either be by a registered user, or an anonymous user
 * Is used in Guestbook, Serie, Match and possibly News.
 * @author Andreas Borglin
 *
 */
@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable="true")
public class Comment implements PersistentData {

	//private static final long serialVersionUID = 2095905532017979351L;

	/* Encoded key generated by GAE when first saved.
	 * Guaranteed to be unique and contains mapping to 
	 * a team using gae.pk-name key defined below. */
	@PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    @Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true")
    private String iEncodedKey;
   
	// Parent can be any entity
    @Persistent
    private String iParentKey;
	
    /* Poster, which can be a registered Person key or anonymous user name */
	@Persistent
	private String iPosterName;
	
	@Persistent
	private String iPersonKey;
	
	@Persistent
	private Date iDate;
	
	@Persistent
	private String iText;
	
	public Comment(){
		
		
	}
	
	public static Comment createCommentFromUserForm(UserContentProvider aProvider){
		
		Comment c = new Comment();
		
		c.setDate(aProvider.getDate(TeamSpirit.getConstants().date()));
        c.setText(aProvider.getString(TeamSpirit.getConstants().text()));
		
        // Player must be set from session somehow
		return c;
	}
	/*
	public Long getCommentId(){
	    return iCommentId;
	}
	
	public void setCommentId(Long aCommentId){
	    iCommentId = aCommentId;
	}*/
	
	/*
    public String getTitle(){
	        return iTitle;
	    }

    public void setTitle(String aTitle) {
        iTitle = aTitle;
    }
    
    public Player getPoster(){
        return iPoster;
    }*/
    
	public void setParentKey(String aKey){
		iParentKey = aKey;
	}
	
	public String getParentKey(){
		return iParentKey;
	}
	
    public void setPoster(String aPosterName, String aPersonKey){
        iPosterName = aPosterName;
        iPersonKey = aPersonKey;
    }
    
    public String getPosterName(){
    	return iPosterName;
    }
    
    public String getPersonKey(){
    	return iPersonKey;
    }
    
    public Date getDate(){
        return iDate;
    }
    
    public void setDate(Date aDate){
        iDate = aDate;
    }
    
    public String getText(){
        return iText;
    }
    
    public void setText(String aText){
        iText = aText;
    }
    
    public String dumpContent(){
        return new String("Dump" +
                "\nId:" + iEncodedKey +
                //"\nTitle:" + iTitle +
                "\nPoster:" + iPosterName +
                "\nDate:" + iDate +
                "\nText:" + iText);
    }

	@Override
	public String getDisplayingWidget() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getEncodedKey() {
		// TODO Auto-generated method stub
		return iEncodedKey;
	}
	
	
}
